package com.ye.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ye.reggie.common.R;
import com.ye.reggie.pojo.Category;
import com.ye.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  分类管理
 *
 * @author 叶旭晖
 * @date 2022/4/20 19:02
 * @param
 * @return null
 */
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     *  新增分类
     *
     * @author 叶旭晖
     * @date 2022/4/20 20:20
     * @param [category]
     * @return R<String>
     */
    @PostMapping
    public R<String> save(@RequestBody Category category){
        categoryService.save(category);
        return R.success("新增分类成功");
    }

    /**
     *  分页查询
     *
     * @author 叶旭晖
     * @date 2022/4/20 20:30
     * @param [page, pageSize]
     * @return R<Page>
     */
    @GetMapping("/page")
    public R<Page> page(Integer page,Integer pageSize){

        Page<Category> pageInfo = new Page<>(page,pageSize);

        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByAsc(Category::getSort);

        Page<Category> categoryPage = categoryService.page(pageInfo, lambdaQueryWrapper);

        return R.success(categoryPage);
    }

    /**
     *  根据id删除分类
     *
     * @author 叶旭晖
     * @date 2022/4/20 20:37
     * @param [id]
     * @return R<String>
     */
    @DeleteMapping
    public R<String> delete(Long id){
//        categoryService.removeById(id);

        categoryService.remove(id);
        return R.success("分类信息删除成功");
    }

    /**
     *  根据id修改分类信息
     *
     * @author 叶旭晖
     * @date 2022/4/20 21:53
     * @param [category]
     * @return R<String>
     */
    @PutMapping
    public R<String> update(@RequestBody Category category){
        categoryService.updateById(category);
        return R.success("修改分类信息成功");
    }

    /**
     *  查询菜品分类
     *
     * @author 叶旭晖
     * @date 2022/4/25 18:40
     * @param [category]
     * @return R<List<Category>>
     */
    @GetMapping("/list")
    public R<List<Category>> list(Category category){
        //条件构造器
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加条件
        lambdaQueryWrapper.eq(category.getType() != null,Category::getType,category.getType());
        //添加排序条件
        lambdaQueryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);

        List<Category> list = categoryService.list(lambdaQueryWrapper);

        return R.success(list);
    }
}
